/* This is a customised template.
 * It overrides some optional methods from EntityMap to allow for testing 
 * with Mocks and fast object creation. Search for #custom# below. 
 */

#set($entity = $Neo.Entity)

#if($Neo.Entity.Namespace)
namespace ${Neo.Entity.Namespace}
{
#end

#region Using statements

using System;
using System.Collections;
using System.Data;
using Neo.Core;
#if($entity.Relationships.Count > 0)
using Neo.Core.Util;
#end
using Neo.Framework;
#foreach($ns in $entity.UsedNamespaces)
#if($ns != $entity.Namespace)
using $ns;
#end
#end

#endregion

#region ${entity.ClassName}Base

/// <summary>
/// Generated table specific Base Class </summary>
/// <remarks> This class is generated by the Neo Framework. Make changes to NeoSupport.vtl.</remarks>
public partial class ${entity.ClassName} : EntityObject
{
#foreach($rel in $entity.ToManyRelationships)
	/// <summary>
	/// A collection of ${rel.ForeignEntity.ClassName} objects referencing this instance.</summary>
	/// <remarks> Objects referencing this instance.</remarks>
	public readonly ObjectRelation<${rel.ForeignEntity.ClassName}> ${rel.DotNetName};
#end
       
	internal ${entity.ClassName}(System.Data.DataRow aRow, Neo.Core.ObjectContext aContext) : base(aRow, aContext)
	{
#foreach($rel in $entity.ToManyRelationships)
		$rel.DotNetName = new ObjectRelation<${rel.ForeignEntity.ClassName}>(this, "$rel.DotNetName");
#end
	}
	
#foreach($attribute in $entity.Attributes)
#if(!$attribute.IsHidden)
	/// <summary>
	/// Property $attribute.DotNetName - #if($attribute.Description.Length == 0)No Description #else $attribute.Description #end</summary>
	/// <remarks> Database Path: ${Neo.Model.Name}.${Neo.Entity.TableName}.${attribute.ColumnName}.</remarks>
	/// <value> $attribute.DotNetType. </value>
#if($attribute.DotNetType == "System.String")
	public virtual $attribute.DotNetType $attribute.DotNetName
	{
		get { return Row["$attribute.ColumnName"] as System.String; }
		set { Row["$attribute.ColumnName"] = (value != null) ? (object)value : (object)DBNull.Value; }
#else
	public virtual $attribute.DotNetType? $attribute.DotNetName
	{
		get { object v = Row["$attribute.ColumnName"]; return ($attribute.DotNetType?)((v != DBNull.Value) ? v : null); }
		set { if (value.HasValue) Row["$attribute.ColumnName"] = value; else Row["$attribute.ColumnName"] = DBNull.Value; }
#end
	}    

#end
#end

#foreach($rel in $entity.ToOneRelationships)
	/// <summary>
	/// Property $rel.DotNetName - gets or sets the $rel.ForeignEntity.ClassName object with a one-to-one relationship to this instance.</summary>
	/// <remarks> Property $rel.DotNetName - gets or sets the $rel.ForeignEntity.ClassName object with a one-to-one relationship to this instance.</remarks>
	/// <value> $rel.ForeignEntity.ClassName. </value>
	public virtual $rel.ForeignEntity.ClassName $rel.DotNetName
	{
		get { object fk = Row["$rel.LocalKey"]; return (fk == DBNull.Value) ? null : ($rel.ForeignEntity.ClassName)GetRelatedObject("$rel.ForeignEntity.TableName", fk); }
		set { SetRelatedObject(value, "$rel.LocalKey", "$rel.ForeignKey" ); }
	}

#end
	/// <summary>
	/// Returns the value associated with the propName parameter.</summary>
	/// <remarks> You will need to cast the returned object into its native type upon return.</remarks>
	/// <param name="propName">String property name.</param>
	/// <returns> System.Object. </returns>
	public override object GetProperty(string propName)
	{
#foreach($attribute in $entity.Attributes)
#if(!$attribute.IsHidden)
		if(propName == "$attribute.DotNetName") 
			return $attribute.DotNetName;
#end
#end
#foreach($rel in $entity.ToOneRelationships)
		if(propName == "$rel.DotNetName") 
			return $rel.DotNetName;
#end

		return base.GetProperty(propName);
	}


#if($entity.ToManyRelationships.Count > 0)
    /// <summary>
    /// Ensures related tables are loaded into memory for a cascaded delete.</summary>
    /// <remarks> Deletes related tables entries when cascade is specified.</remarks>
    public override void Delete() 
    {
#foreach($rel in $entity.ToManyRelationships)
#if($rel.DeleteRule.ToString() == "Cascade")
        ${rel.DotNetName}.Touch();
        while (${rel.DotNetName}.Count > 0)
        {
            ${rel.DotNetName}[0].Delete();
        }
#end
#end
        base.Delete();
    }
#end
	
	/// <summary>
    /// Populates the EntityObject from the specified surrogate.
    /// </summary>
    /// <param name="surrogate">The surrogate.</param>
	public void Populate(${entity.ClassName}Surrogate surrogate)
	{
#foreach($attribute in $entity.Attributes)
#if(!$attribute.IsHidden && !$attribute.IsPkColumn)
		${attribute.DotNetName} = surrogate.${attribute.DotNetName};
#end
#end
	}
}

#endregion

#region ${entity.ClassName}Template

/// <summary>
/// Generated table specific Query Template Class </summary>
/// <remarks> A Query Template for ${entity.ClassName} operations. </remarks>
public class ${entity.ClassName}Template : QueryTemplate
{

	/// <summary>
	/// Constructor </summary>
	/// <remarks> Public Constructor.</remarks>
	/// <param name="anEntityMap">IEntityMap instance.</param>
	public ${entity.ClassName}Template(IEntityMap anEntityMap) : base(anEntityMap)
	{
	}
	
#foreach($attributes in $entity.Attributes)
#if(!$attributes.IsHidden)
	/// <summary>
	/// Property $attributes.DotNetName - #if($attributes.Description.Length == 0)No Description #else $attributes.Description #end</summary>
	/// <remarks> Database Path: ${Neo.Entity.Model.Name}.${Neo.Entity.TableName}.${attributes.ColumnName}.</remarks>
	/// <value> $attributes.DotNetType. </value>
	public $attributes.DotNetType $attributes.DotNetName
	{
		get { return ($attributes.DotNetType)QueryValues["$attributes.DotNetName"]; }
		set { QueryValues["$attributes.DotNetName"] = value; }
	}

#end
#end
#foreach($rel in $entity.ToOneRelationships)
	/// <summary>
	/// Property $rel.DotNetName - gets or sets the $rel.ForeignEntity.ClassName object with a one-to-one relationship to this instance.</summary>
	/// <remarks> Gets or sets the $rel.ForeignEntity.ClassName object.</remarks>
	/// <value> $rel.ForeignEntity.ClassName. </value>
	public $rel.ForeignEntity.ClassName $rel.DotNetName
	{
		get { return ($rel.ForeignEntity.ClassName)QueryValues["$rel.DotNetName"]; }
		set { QueryValues["$rel.DotNetName"] = value; }
	}

#end
               
}

#endregion

#region ${entity.ClassName}Factory

/// <summary>
/// Factory class for the creation of ${entity.ClassName} objects.</summary>
/// <remarks> This class is generated by the Neo Framework. Make changes to NeoSupport.vtl.</remarks>
public class ${entity.ClassName}Factory : Neo.Framework.ObjectFactory
{

	/// <summary>
	/// Constructor requiring an ObjectContext parameter.</summary>
	/// <remarks> Main entry point for the retrieval and creation of new ${entity.ClassName} objects.</remarks>
	/// <param name="context">ObjectContext.</param>
	public ${entity.ClassName}Factory(ObjectContext context) : base(context, typeof($entity.ClassName))
	{
	}

#if($entity.IdMethod.ToString == "None")
#if($entity.PrimaryKeyIsForeignKey)
#foreach($combination in $entity.RelationshipSetsForColumns($entity.PkColumns))
	/// <summary>
	/// Create new $entity.ClassName object.</summary>
	/// <remarks>IdMethod = $entity.IdMethod.ToString(), PrimaryKeyIsForeignKey = $entity.PrimaryKeyIsForeignKey.ToString(), RelationshipSetsForColumns = $entity.RelationshipSetsForColumns($entity.PkColumns).Length.</remarks>
#foreach($rel in $combination)#set( $pnum = $velocityCount - 1)
	/// <param name="arg${pnum}">$rel.ForeignEntity.ClassName.</param>
#end
	/// <returns> $entity.ClassName </returns>
	public $entity.ClassName CreateObject(#foreach($rel in $combination)#set( $pnum = $velocityCount - 1)#if( $pnum > 0), #end$rel.ForeignEntity.ClassName arg${pnum}#end)
	{
		return ($entity.ClassName)base.CreateObject(new object[] {#foreach($rel in $combination)#set( $pnum = $velocityCount - 1)#if( $pnum > 0),#end arg${pnum}.Row["$rel.ForeignKey"]#end } );
	}

#end
#else
	/// <summary>
	/// Create new $entity.ClassName object.</summary>
	/// <remarks>IdMethod = $entity.IdMethod.ToString(), PrimaryKeyIsForeignKey = $entity.PrimaryKeyIsForeignKey.ToString(), RelationshipSetsForColumns = $entity.RelationshipSetsForColumns($entity.PkColumns).Length.</remarks>
#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)
	/// <param name="arg${pnum}">#if( $entity.ColumnIsForeignKey($column.ColumnName))$entity.RelationshipForAttribute($column).ForeignEntity.ClassName#else$column.Description#end.</param>
#end	
	/// <returns> $entity.ClassName </returns>
	public $entity.ClassName CreateObject(#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)#if( $pnum > 0), #end#if( $entity.ColumnIsForeignKey($column.ColumnName))$entity.RelationshipForAttribute($column).ForeignEntity.ClassName#else$column.DotNetType#end arg${pnum}#end)
	{
		return ($entity.ClassName)base.CreateObject(new object[] {#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)#if( $entity.ColumnIsForeignKey($column.ColumnName)) arg${pnum}.Row["$entity.RelationshipForAttribute($column).ForeignKey"]#else arg${pnum}#end#if( $velocityCount < $entity.PkColumns.Count),#end#end } );
	}
	
#end
#else	 
	/// <summary>
	/// Create new $entity.ClassName object.</summary>
	/// <remarks>IdMethod = $entity.IdMethod.ToString(), PrimaryKeyIsForeignKey = $entity.PrimaryKeyIsForeignKey.ToString(), RelationshipSetsForColumns = $entity.RelationshipSetsForColumns($entity.PkColumns).Length.</remarks>
	/// <returns> $entity.ClassName </returns>
	public $entity.ClassName CreateObject()
	{
		return ($entity.ClassName)base.CreateObject(null);
	}
	
#end 

	/// <summary>
	/// Find a specified $entity.ClassName object.</summary>
	/// <remarks> Requires Primary Key values and/or parent object instances.</remarks>
#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)
	/// <param name="arg${pnum}">#if( $entity.ColumnIsForeignKey($column.ColumnName))$entity.RelationshipForAttribute($column).ForeignEntity.ClassName#else$column.Description#end.</param>
#end
	/// <returns> $entity.ClassName </returns>
	public $entity.ClassName FindObject(#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)#if( $pnum > 0), #end#if( $entity.ColumnIsForeignKey($column.ColumnName))$entity.RelationshipForAttribute($column).ForeignEntity.ClassName#else$column.DotNetType#end arg${pnum}#end)
	{
		return ($entity.ClassName)base.FindObject(new object[] {#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)#if( $entity.ColumnIsForeignKey($column.ColumnName)) arg${pnum}.Row["$entity.RelationshipForAttribute($column).ForeignKey"]#else arg${pnum}#end#if( $velocityCount < $entity.PkColumns.Count),#end#end } );
	}

	///<summary>
	///Find a specified $entity.ClassName object.</summary>
	///<remarks> Requires Primary Key values and/or parent object instances.</remarks>
#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)
	///<param name="arg${pnum}">#if( $entity.ColumnIsForeignKey($column.ColumnName))$entity.RelationshipForAttribute($column).ForeignEntity.ClassName#else$column.Description#end.</param>
#end
	///<returns> $entity.ClassName </returns>
	public $entity.ClassName FindObjectByPK(#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)#if( $pnum > 0),#end $column.DotNetType arg${pnum} #end)
	{
		return ($entity.ClassName) base.FindObject( new object[] { #foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1) arg${pnum}#if( $velocityCount < $entity.PkColumns.Count),#end#end } );
	}
	
#set ($doNullable = false)
#foreach($column in $entity.PkColumns)
#if ($column.DotNetType != "System.String")
#set ($doNullable = true)
#end
#end

#if ($doNullable)
	///<summary>
	///Find a specified $entity.ClassName object.</summary>
	///<remarks> Requires Primary Key values and/or parent object instances.</remarks>
#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)
	///<param name="arg${pnum}">#if( $entity.ColumnIsForeignKey($column.ColumnName))$entity.RelationshipForAttribute($column).ForeignEntity.ClassName#else$column.Description#end.</param>
#end
	///<returns> $entity.ClassName </returns>
	public $entity.ClassName FindObjectByPK(#foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1)#if( $pnum > 0),#end ${column.DotNetType}#if ($column.DotNetType != "System.String")?#end arg${pnum} #end)
	{
		return ($entity.ClassName) base.FindObject( new object[] { #foreach($column in $entity.PkColumns)#set( $pnum = $velocityCount - 1) arg${pnum}#if ($column.DotNetType != "System.String").Value#end#if( $velocityCount < $entity.PkColumns.Count),#end#end } );
	}
#end

	/// <summary>
	/// Find a list of $entity.ClassName objects.</summary>
	/// <remarks> Returns all.</remarks>
	/// <returns> ObjectList&lt;${entity.ClassName}&gt; </returns>
	public new ObjectList<${entity.ClassName}> FindAllObjects()
	{
		ObjectList<${entity.ClassName}> c = new ObjectList<${entity.ClassName}>();
		foreach(${entity.ClassName} eo in base.FindAllObjects())
			c.Add(eo);
#if($entity.DefaultProperty)
		c.Sort("$entity.DefaultProperty", SortDirection.Ascending);
#end
		return c;
	}
	
	/// <summary>
	/// Return the correct Query Template for the $entity.ClassName object.</summary>
	/// <remarks> A query template is used for creating SQL like queries, in a more OO fashion.</remarks>
	/// <returns> ${entity.ClassName}Template </returns>
	public ${entity.ClassName}Template GetQueryTemplate()
	{
		return new ${entity.ClassName}Template(EntityMap);
	}
	
	/// <summary>
	/// SQL like query to find and return a list of objects based on a template.</summary>
	/// <remarks> A query template is used for creating SQL like queries, in a more OO fashion.</remarks>
	/// <param name="template">${entity.ClassName}Template.</param>
	/// <returns> ObjectList&lt;${entity.ClassName}&gt; </returns>
	public ObjectList<${entity.ClassName}> Find(${entity.ClassName}Template template)
	{
		ObjectList<${entity.ClassName}> c = new ObjectList<${entity.ClassName}>();
		foreach(${entity.ClassName} eo in base.Find(template))
			c.Add(eo);
		return c;
	}

	/// <summary>
	/// SQL like query to find and return a list of objects based on a FetchSpecification.</summary>
	/// <remarks> A query Fetch Specification is used for creating SQL like queries, in a more OO fashion.</remarks>
	/// <param name="fetchSpecification">FetchSpecification.</param>
	/// <returns> ObjectList&lt;${entity.ClassName}&gt; </returns>
	public ObjectList<${entity.ClassName}> Find(FetchSpecification fetchSpecification)
	{
		ObjectList<${entity.ClassName}> c = new ObjectList<${entity.ClassName}>();
		foreach(${entity.ClassName} eo in base.Find(fetchSpecification))
			c.Add(eo);
		return c;
	}
	
	/// <summary>
    /// SQL like query to find and return a list of objects.</summary>
    /// <remarks> For example: ...Find("TitleAuthors.(Title = {0} or Title.Publisher = {1})", title, publisher).</remarks>
    /// <param name="q">The Qualifier.</param>
    /// <returns>ObjectList</returns>
    public new ObjectList<${entity.ClassName}> Find(Qualifier q)
    {
        ObjectList<${entity.ClassName}> c = new ObjectList<${entity.ClassName}>();
        foreach (${entity.ClassName} eo in base.Find(q))
            c.Add(eo);
        return c;
    }
    
	/// <summary>
	/// SQL like query to find and return a list of objects.</summary>
	/// <remarks> For example: ...Find("TitleAuthors.(Title = {0} or Title.Publisher = {1})", title, publisher).</remarks>
	/// <param name="qualifierFormat">String.</param>
	/// <param name="parameters">params object[].</param>
	/// <returns> ObjectList&lt;${entity.ClassName}&gt; </returns>
	public new ObjectList<${entity.ClassName}> Find(string qualifierFormat, params object[] parameters)
	{
		ObjectList<${entity.ClassName}> c = new ObjectList<${entity.ClassName}>();
		foreach(${entity.ClassName} eo in base.Find(qualifierFormat, parameters))
			c.Add(eo);
		return c;
	}

	/// <summary>
	/// SQL like query to find and return a list of objects limiting the munber of items returned.</summary>
	/// <remarks> For example: ...Find(10, "TitleAuthors.(Title = {0} or Title.Publisher = {1})", title, publisher).</remarks>
	/// <param name="limit">Int32.</param>
	/// <param name="qualifierFormat">String.</param>
	/// <param name="parameters">params object[].</param>
	/// <returns> ObjectList&lt;${entity.ClassName}&gt; </returns>
	public new ObjectList<${entity.ClassName}> FindWithLimit(int limit, string qualifierFormat, params object[] parameters)
	{
		ObjectList<${entity.ClassName}> c = new ObjectList<${entity.ClassName}>();
		foreach(${entity.ClassName} eo in base.Find(qualifierFormat, parameters))
			c.Add(eo);
		return c;
	}
	
	/// <summary>
	/// SQL like query to find and return an object.</summary>
	/// <remarks> For example: ...FindFirst("TitleAuthors.(Title = {0} or Title.Publisher = {1})", title, publisher).</remarks>
	/// <param name="qualifierFormat">String.</param>
	/// <param name="parameters">params object[].</param>
	/// <returns> ${entity.ClassName}. </returns>
	public new ${entity.ClassName} FindFirst(string qualifierFormat, params object[] parameters)
	{
		return (${entity.ClassName})base.FindFirst(qualifierFormat, parameters);
	}

    /// <summary>
    /// SQL like query to find and return an object.</summary>
    /// <remarks> For example: ...FindFirst("TitleAuthors.(Title = {0} or Title.Publisher = {1})", title, publisher).</remarks>
    /// <param name="q">The Qualifier.</param>
    /// <returns>${entity.ClassName}</returns>
    public new ${entity.ClassName} FindFirst(Qualifier q)
    {
        return (${entity.ClassName})base.FindFirst(q);
    }
    
	/// <summary>
	/// SQL like query to find and return an object.</summary>
	/// <remarks> For example: ...FindUnique("TitleAuthors.(Title = {0} or Title.Publisher = {1})", title, publisher).</remarks>
	/// <param name="qualifierFormat">String.</param>
	/// <param name="parameters">params object[].</param>
	/// <returns> ${entity.ClassName}. </returns>
	public new ${entity.ClassName} FindUnique(string qualifierFormat, params object[] parameters)
	{
		return (${entity.ClassName})base.FindUnique(qualifierFormat, parameters);
	}	

	
}

#endregion

#region ${entity.ClassName}EntityMap

//  -------- Entity Map -----------------------------------------------------
/// <summary>Creates the in memory version of the dataset for the ${entity.ClassName} DOM.</summary>
/// <remarks>Generated by Neo Framework.</remarks>
internal class ${entity.ClassName}Map : EntityMap
{
	  /// <summary>Array of Primary Key column names.</summary>
	  /// <remarks>Primary Key columns.</remarks>
    private static readonly string[] pkcolumns = {#foreach($column in $entity.PkColumns)#if( $velocityCount > 1),#end "$column.ColumnName"#end };
    /// <summary>Array of Database column names.</summary>
	  /// <remarks>Database column names.</remarks>
    private static readonly string[] columns = {#foreach($column in $entity.Attributes)#if(!$attributes.IsHidden)#if( $velocityCount > 1),#end "$column.ColumnName"#end#end };
    /// <summary>Array of DOM object properties.</summary>
	  /// <remarks>DOM object properties.</remarks>
    private static readonly string[] attributes = {#foreach($column in $entity.Attributes)#if(!$attributes.IsHidden)#if( $velocityCount > 1),#end "$column.DotNetName"#end#end };
    /// <summary>Array of Related Object names.</summary>
	  /// <remarks>Related Object names.</remarks>
    private static readonly string[] relations = {#foreach($rel in $entity.Relationships)#if( $velocityCount > 1),#end "$rel.DotNetName"#end };

    private Type concreteObjectType = typeof($entity.ClassName);

	  /// <summary>Property ObjectType.</summary>
	  /// <remarks>Returns the DOM's Type.</remarks>
	  /// <value>System.Type.</value>
    public override System.Type ObjectType
    {
        get { return typeof($entity.ClassName); }
    }
    
    /// <summary>Property ObjectType.</summary>
	  /// <remarks>Returns the DOM's Type.</remarks>
	  /// <value>System.Type.</value>
    public override System.Type ConcreteObjectType
    {
        get { return concreteObjectType; }										 /* #custom# overridden */
        set { concreteObjectType = value; Factory.AddCustomType(value, this); }  /* #custom# overridden */
    }
    
    /// <summary>Property TableName.</summary>
	  /// <remarks>Returns database table name.</remarks>
	  /// <value>string.</value>
    public override string TableName
    {
        get { return "${entity.TableName}"; }
    }
    
    /// <summary>Property PrimaryKeyColumns.</summary>
	/// <remarks>Array of Primary Key column names.</remarks>
	/// <value>string[].</value>
    public override string[] PrimaryKeyColumns
    {
        get { return pkcolumns; }
    }

	/// <summary>Property Columns.</summary>
	/// <remarks>Array of Database column names.</remarks>
	/// <value>string[].</value>
    public override string[] Columns
    {
        get { return columns; }
    }

	/// <summary>Property Attributes.</summary>
	/// <remarks>Array of DOM object properties.</remarks>
	/// <value>string[].</value>
    public override string[] Attributes
    {
        get { return attributes; }
    }

    /// <summary>Property Relations.</summary>
	/// <remarks>Array of Related Object names.</remarks>
	/// <value>string[].</value>
    public override string[] Relations
    {
        get { return relations; }
    }

	/// <summary>Generates a Hashtable, keyed by Object Name, or information regarding related objects.</summary>
	/// <remarks>Related Objects.</remarks>
	/// <returns>IDictionary.</returns>
  	protected override IDictionary GetRelationInfos()
    {
    	IDictionary infos = new Hashtable($entity.Relationships.Count);
 #foreach($rel in $entity.Relationships)
 #if($rel.Direction.ToString == "Parent")
		infos.Add("$rel.DotNetName", new RelationInfo(Factory, typeof($rel.LocalEntity.ClassName), typeof($rel.ForeignEntity.ClassName), "$rel.LocalKey", "$rel.ForeignKey"));
#else
		infos.Add("$rel.DotNetName", new RelationInfo(Factory, typeof($rel.ForeignEntity.ClassName), typeof($rel.LocalEntity.ClassName), "$rel.ForeignKey", "$rel.LocalKey"));
#end
#end
		return infos;    	
    }
    
	/// <summary>Based upon the model specified Id Method, returns the proper PkInitializer.</summary>
	/// <remarks>The object used to create a new PK value.</remarks>
	/// <returns>IPkInitializer.</returns>
	public override IPkInitializer GetPkInitializer()
	{
#if($entity.IdMethod.ToString == "None")
		return new UserPkInitializer();
#elseif($entity.IdMethod.ToString == "Native")
		return new NativePkInitializer();
#elseif($entity.IdMethod.ToString == "IdBroker")
		return new IdBrokerPkInitializer();
#elseif($entity.IdMethod.ToString == "Guid")
		return new GuidPkInitializer();
#end
	}
    
	/// <summary>Creates a new instance of ${entity.ClassName}.</summary>
	/// <remarks>Create new ${entity.ClassName}.</remarks>
	/// <param name="row">DataRow.</param>
	/// <param name="context">ObjectContext.</param>
	/// <returns>IEntityObject.</returns>
    public override IEntityObject CreateInstance(DataRow row, ObjectContext context) /* #custom# overridden */
    {
		if(ConcreteObjectType != ObjectType)
			return base.CreateInstance(row, context);
		return new ${entity.ClassName}(row, context);
	}
    
    /// <summary>Generates the in memory dataset.</summary>
	/// <remarks>Called by the Factory object.</remarks>
	/// <param name="table">DataTable.</param>
    protected override void WriteBasicSchema(DataTable table)
    {
		DataColumn		c;
		
#foreach($attributes in $entity.Attributes)
		c = table.Columns.Add("$attributes.ColumnName", typeof($attributes.DotNetType));
#if($attributes.IsPkColumn)
#if($entity.PkColumns.Count == 1) 
		c.Unique = true;
#end
#if(($entity.IdMethod.ToString == "Native") || ($entity.IdMethod.ToString == "IdBroker"))
		c.AutoIncrement = true; c.AutoIncrementSeed = c.AutoIncrementStep = -1;
#end
#end
#if($attributes.AllowsNull)
		c.AllowDBNull = true;
#end
#if($attributes.DefaultValue)
#if($attributes.DefaultValue.Equals("now"))
        c.DefaultValue = Convert.ChangeType(System.DateTime.Now, typeof($attributes.DotNetType));
#else
        c.DefaultValue = Convert.ChangeType("$attributes.DefaultValue", typeof($attributes.DotNetType));
#end
#end
#if($attributes.IsSQLTextColumn)
		c.ExtendedProperties.Add("LockStrategy","NONE");
#end
#end
		table.PrimaryKey = new DataColumn[] {#foreach($column in $entity.PkColumns)#if( $velocityCount > 1),#end table.Columns["$column.ColumnName"]#end };
	}
	
#if($entity.Relationships.Count > 0)
	/// <summary>Generates the dataset relationships.</summary>
	/// <remarks>Dataset Relationships.</remarks>
	/// <param name="table">DataTable.</param>
	protected override void WriteRelations(DataTable table)
	{
		DataRelation r;
		
#foreach($rel in $entity.Relationships)
#if($rel.Direction.ToString == "Parent") ## yes, that's how you compare enums...
		if(table.DataSet.Relations["${rel.LocalEntity.TableName}*${rel.ForeignEntity.TableName}.${rel.ForeignKey}"] == null)
		{
			r = table.DataSet.Relations.Add("${rel.LocalEntity.TableName}*${rel.ForeignEntity.TableName}.${rel.ForeignKey}", 
					table.DataSet.Tables["$rel.LocalEntity.TableName"].Columns["$rel.LocalKey"],
					table.DataSet.Tables["$rel.ForeignEntity.TableName"].Columns["$rel.ForeignKey"]);
			r.ChildKeyConstraint.UpdateRule = $rel.DotNetUpdateRule;
			r.ChildKeyConstraint.DeleteRule = $rel.DotNetDeleteRule;
		}
#else
		if(table.DataSet.Relations["${rel.ForeignEntity.TableName}*${rel.LocalEntity.TableName}.${rel.LocalKey}"] == null)
		{
			r = table.DataSet.Relations.Add("${rel.ForeignEntity.TableName}*${rel.LocalEntity.TableName}.${rel.LocalKey}", 
					table.DataSet.Tables["$rel.ForeignEntity.TableName"].Columns["$rel.ForeignKey"],
					table.DataSet.Tables["$rel.LocalEntity.TableName"].Columns["$rel.LocalKey"]);
#set($rel = $rel.InverseRelationship)
#if($rel)
			r.ChildKeyConstraint.UpdateRule = $rel.DotNetUpdateRule;
			r.ChildKeyConstraint.DeleteRule = $rel.DotNetDeleteRule;
#else
			r.ChildKeyConstraint.UpdateRule = Rule.Cascade;
			r.ChildKeyConstraint.DeleteRule = Rule.SetNull; /* child and can't find inverse */
#end
		}
#end			
#end
	}
#end
}

#endregion

#region ${Neo.Entity.ClassName}SurrogateBase

/// <summary>Represents an entity object disconnected from the database.</summary>
/// <remarks>
///   <para>Database Path: ${Neo.Entity.Model.Name}.${Neo.Entity.TableName}.</para>
/// </remarks>
[Serializable]
public abstract class ${Neo.Entity.ClassName}SurrogateBase : EntityObjectSurrogate
{
    /// <summary>Default constructor.</summary>
    public ${Neo.Entity.ClassName}SurrogateBase()
    {
#foreach($attribute in $entity.Attributes)#if(!$attribute.IsHidden || ($attribute.IsHidden && $attribute.IsPkColumn))#if($attribute.DefaultValue && $attribute.DefaultValue != "")
#if($attribute.DotNetType.Equals("System.String"))
        _${attribute.DotNetName.ToLower()} = "${attribute.DefaultValue}";
#end
#if($attribute.DotNetType.Equals("System.DateTime"))
#if($attribute.DefaultValue.Equals("now"))
        _${attribute.DotNetName.ToLower()} = System.DateTime.Now;
#else
        _${attribute.DotNetName.ToLower()} = System.Convert.ToDateTime("${attribute.DefaultValue}");
#end
#else
        _${attribute.DotNetName.ToLower()} = ($attribute.DotNetType)System.Convert.ChangeType("${attribute.DefaultValue}", typeof($attribute.DotNetType));
#end
#end##end has default value
#end##end hidden check
#end##end foreach
    }
    
    /// <summary>
    /// Initializes a new instance of the <see cref="${Neo.Entity.ClassName}SurrogateBase"/> class.
    /// </summary>
    /// <param name="entity">The entity.</param>
    public ${Neo.Entity.ClassName}SurrogateBase(${Neo.Entity.ClassName} entity)
    {
#foreach($attribute in $entity.Attributes)
#if(!$attribute.IsHidden)
		${attribute.DotNetName} = entity.${attribute.DotNetName};
#end
#end
    }
    
    /// <summary>
    /// Updates the entity with the values contained within the surrogate.
    /// </summary>
    /// <param name="entity">The entity.</param>
    public void BindObjectInstance(${Neo.Entity.ClassName} entity)
    {
		if (entity.Row.RowState == DataRowState.Added)
        {
#foreach($attribute in $entity.Attributes)
#if($attribute.IsPkColumn == false && $attribute.IsHidden == false)
			entity.${attribute.DotNetName} = ${attribute.DotNetName};
#end
#end
		}
		else
		{
#foreach($attribute in $entity.Attributes)
#if($attribute.IsPkColumn == false && $attribute.IsHidden == false)		
			if (_${attribute.DotNetName.ToLower()}HasBeenUpdated)
			{
				entity.${attribute.DotNetName} = ${attribute.DotNetName};
			}
#end
#end
		}
    }
    
#foreach($rel in $entity.ToManyRelationships)
    /// <summary>
    /// Property $rel.DotNetName </summary>
    /// <remarks> </remarks>
    /// <value> System.Collections.ArrayList </value>
    public System.Collections.ArrayList ${rel.DotNetName}
    {
    	get {return _${rel.DotNetName.ToLower()};}
    	set {_${rel.DotNetName.ToLower()} = value;}
    }
		
	private System.Collections.ArrayList _${rel.DotNetName.ToLower()};
	
#end
#foreach($attribute in $entity.Attributes)
#if(!$attribute.IsHidden)
#if($attribute.DotNetType == "System.String")
    /// <summary>
    /// Property $attribute.DotNetName - #if($attribute.Description.Length == 0)No Description #else $attribute.Description #end</summary>
    /// <remarks> Database Path: ${Neo.Model.Name}.${Neo.Entity.TableName}.${attribute.ColumnName}.</remarks>
    /// <value> $attribute.DotNetType. </value>
    public $attribute.DotNetType ${attribute.DotNetName}
    {
    	get {return _${attribute.DotNetName.ToLower()};}
#if($attribute.IsPkColumn)
		set {_${attribute.DotNetName.ToLower()} = value;}
#else
    	set {_${attribute.DotNetName.ToLower()} = value; _${attribute.DotNetName.ToLower()}HasBeenUpdated = true;}
#end
    }

	private $attribute.DotNetType _${attribute.DotNetName.ToLower()};
#if($attribute.IsPkColumn == false)
    private bool _${attribute.DotNetName.ToLower()}HasBeenUpdated;
#end
	
#else
	/// <summary>
    /// Property $attribute.DotNetName - #if($attribute.Description.Length == 0)No Description #else $attribute.Description #end</summary>
    /// <remarks> Database Path: ${Neo.Model.Name}.${Neo.Entity.TableName}.${attribute.ColumnName}.</remarks>
    /// <value> $attribute.DotNetType. </value>
    public $attribute.DotNetType? ${attribute.DotNetName}
    {
    	get {return _${attribute.DotNetName.ToLower()};}
#if($attribute.IsPkColumn)
		set {_${attribute.DotNetName.ToLower()} = value;}
#else
    	set {_${attribute.DotNetName.ToLower()} = value; _${attribute.DotNetName.ToLower()}HasBeenUpdated = true;}
#end
    }

	private $attribute.DotNetType? _${attribute.DotNetName.ToLower()};
#if($attribute.IsPkColumn == false)
    private bool _${attribute.DotNetName.ToLower()}HasBeenUpdated;
#end
	
#end
#end
#end
#foreach($rel in $entity.ToOneRelationships)
    /// <summary>
    /// Property $rel.DotNetName - gets or sets the $rel.ForeignEntity.ClassName object with a one-to-one relationship to this instance.</summary>
    /// <remarks> Property $rel.DotNetName - gets or sets the $rel.ForeignEntity.ClassName object with a one-to-one relationship to this instance.</remarks>
    /// <value> $rel.ForeignEntity.ClassName. </value>
    public ${rel.ForeignEntity.ClassName}Surrogate ${rel.DotNetName}
    {
    	get {return _${rel.DotNetName.ToLower()};}
    	set {_${rel.DotNetName.ToLower()} = value;}
    }

	private ${rel.ForeignEntity.ClassName}Surrogate _${rel.DotNetName.ToLower()};
		
#end

    /// <summary>
    /// Binds the surrogate values to the search template.
    /// </summary>
    /// <param name="emptyTemplate">The empty template.</param>
	public void BindToSearchTemplate(${Neo.Entity.ClassName}Template emptyTemplate)
    {
#foreach($rel in $entity.ToOneRelationships)

#foreach($at in $rel.LocalEntity.Attributes)
#if($at.ColumnName == $rel.LocalKey)
#if(!$at.IsHidden)
#if($at.DotNetType == "System.String")
        if (this.${at.PropertyName} != null)
            emptyTemplate.${at.PropertyName} = this.${at.PropertyName};
#else
        if (this.${at.PropertyName} != null)
            emptyTemplate.${at.PropertyName} = this.${at.PropertyName}.Value;
#end
#end
#end
#end
#end
    }
    
	/// <summary>
	/// Determines whether this instance is valid.
	/// </summary>
	/// <returns>
	///	<c>true</c> if this instance is valid; otherwise, <c>false</c>.
	/// </returns>
    public abstract bool IsValid();

	/// <summary>
	/// Gets the validation message.
	/// </summary>
	/// <value>The validation message.</value>
    public abstract String ValidationMessage{get;}

}
#endregion

#if($Neo.Entity.Namespace)
}
#end

